package example.java.lang.reflect.InvocationHandler;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;

import org.apache.log4j.Logger;

public class CalculatorLoggingHandler implements InvocationHandler {
	private Logger log = Logger.getLogger(getClass());
	
	private Object target;
	
	public CalculatorLoggingHandler(Object target) {
		super();
		this.target = target;
	}

	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		//Log the method beginning with the method name and arguments.
		log.info("The method " + method.getName() + "() begins with " + Arrays.toString(args));
		
		//Perform the actual calculation on the target calculator object by calling Method.invoke() and passing in the target object and method arguments.
		Object result = method.invoke(target, args);
		
		//Log the method ending with the returning result.
		log.info("The method " + method.getName() + "() ends with " + result);
		
		return result;
	}

	public static Object createProxy(Object target){
		return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), new CalculatorLoggingHandler(target));
	}
}
